﻿<UserControl x:Class="Views.ViewEstimateurs" 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:System="clr-namespace:System;assembly=mscorlib" 
             xmlns:VMtxgar="clr-namespace:OMANE.Modules.TauxGarantis.ViewModels"
             DataContext="{Binding Source={x:Static VMtxgar:Locator.ViewModelEstimateursStatic}}"
             xmlns:Theme="clr-namespace:OMANE.Common.UI.Theme;assembly=OMANE.Common.UI.Theme"
             mc:Ignorable="d"   
             >
    <UserControl.Resources>
        <HorizontalAlignment x:Key="EntryLabelHorizontalAlignment">Right</HorizontalAlignment>
        <Thickness x:Key="EntryLabelMargin">0,0,5,0</Thickness>
        <VerticalAlignment x:Key="EntryLabelVerticalAlignment">Center</VerticalAlignment>
        <Thickness x:Key="EntryGridMargin">0,2</Thickness>
        <HorizontalAlignment x:Key="EntryValueHorizontalAlignment">Left</HorizontalAlignment>
        <Thickness x:Key="EntryValueMargin">5,0,0,0</Thickness>
        <VerticalAlignment x:Key="EntryValueVerticalAlignment">Center</VerticalAlignment>
        <System:Double x:Key="EntryValueWidth">149</System:Double>
    </UserControl.Resources>

    <Grid DataContext="{Binding Mode=OneWay}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition Width="10"/>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="10"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid Grid.ColumnSpan="3" >
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition Height="Auto"/>

            </Grid.RowDefinitions>

            <GroupBox Style="{DynamicResource OMANE_GpeBox}" Header="Estimateurs" Margin="10" Focusable="True" IsTabStop="True" TabIndex="0">
                <StackPanel HorizontalAlignment="Center" Margin="0,10" >
                    <UniformGrid Margin="{DynamicResource EntryGridMargin}" Columns="2" >
                        <TextBlock Text="Durée en nombre  de mois" Margin="{DynamicResource EntryLabelMargin}" HorizontalAlignment="{DynamicResource EntryLabelHorizontalAlignment}" VerticalAlignment="{DynamicResource EntryLabelVerticalAlignment}" />
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="{DynamicResource EntryValueHorizontalAlignment}" VerticalAlignment="{DynamicResource EntryValueVerticalAlignment}" DataContext="{Binding DureeEnNombreDeMois}">
                            <Theme:NumericEditor 
                            Style="{StaticResource DureeMoisEditor}" 
                            Value="{Binding UniqueValue, UpdateSourceTrigger=LostFocus}"
                            Format="0 mois"
                            IsEnabled="{Binding NotUsedSegmentation}"
                            ValueType="{x:Type System:Int32}"/>
                            <RadioButton DataContext="{Binding}" Margin="5" VerticalAlignment="Center" Checked="RadioButton_Checked" GroupName="Segments" IsThreeState="False" IsChecked="{Binding Selected, Mode=TwoWay}" Focusable="False" IsTabStop="False"/>

                            <ToggleButton  Style="{StaticResource SegmentEditorToggleButtonStyle}" IsChecked="{Binding NotUsedSegmentation}" Unchecked="ToggleButton_Unchecked" Focusable="False" IsTabStop="False" />
                        </StackPanel>
                    </UniformGrid>
                    <UniformGrid Margin="{DynamicResource EntryGridMargin}" Columns="2" >
                        <TextBlock Text="Taux  nominal" Margin="{DynamicResource EntryLabelMargin}" HorizontalAlignment="{DynamicResource EntryLabelHorizontalAlignment}" VerticalAlignment="{DynamicResource EntryLabelVerticalAlignment}" />
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="{DynamicResource EntryValueHorizontalAlignment}" VerticalAlignment="{DynamicResource EntryValueVerticalAlignment}" DataContext="{Binding TauxNominal}">
                            <Theme:NumericEditor Style="{StaticResource PercentageEditor}" Value="{Binding UniqueValue, UpdateSourceTrigger=LostFocus}" IsEnabled="{Binding NotUsedSegmentation}"/>
                            <RadioButton DataContext="{Binding}" Margin="5" VerticalAlignment="Center" Checked="RadioButton_Checked" GroupName="Segments" IsThreeState="False" IsChecked="{Binding Selected, Mode=TwoWay}" Focusable="False" IsTabStop="False"/>

                            <ToggleButton  Style="{StaticResource SegmentEditorToggleButtonStyle}" IsChecked="{Binding NotUsedSegmentation}" Unchecked="ToggleButton_Unchecked" Focusable="False" IsTabStop="False" />
                        </StackPanel>
                    </UniformGrid>
                    <UniformGrid Margin="{DynamicResource EntryGridMargin}" Columns="2" >
                        <TextBlock Text="Taux de mortalités" Margin="{DynamicResource EntryLabelMargin}" HorizontalAlignment="{DynamicResource EntryLabelHorizontalAlignment}" VerticalAlignment="{DynamicResource EntryLabelVerticalAlignment}" />
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="{DynamicResource EntryValueHorizontalAlignment}" VerticalAlignment="{DynamicResource EntryValueVerticalAlignment}" DataContext="{Binding TauxDeMortalite}">
                            <Theme:NumericEditor Style="{StaticResource PercentageEditor}" Value="{Binding UniqueValue, UpdateSourceTrigger=LostFocus}" IsEnabled="{Binding NotUsedSegmentation}"/>
                            <RadioButton DataContext="{Binding}" Margin="5" VerticalAlignment="Center" Checked="RadioButton_Checked" GroupName="Segments" IsThreeState="False" IsChecked="{Binding Selected, Mode=TwoWay}" Focusable="False" IsTabStop="False"/>

                            <ToggleButton  Style="{StaticResource SegmentEditorToggleButtonStyle}" IsChecked="{Binding NotUsedSegmentation}" Unchecked="ToggleButton_Unchecked" Focusable="False" IsTabStop="False" />
                        </StackPanel>
                    </UniformGrid>

                    <UniformGrid Margin="{DynamicResource EntryGridMargin}" Columns="2" IsEnabled="False" Visibility="Hidden" >
                        <TextBlock Text="Nombre initial d'assurés" Margin="{DynamicResource EntryLabelMargin}" HorizontalAlignment="{DynamicResource EntryLabelHorizontalAlignment}" VerticalAlignment="{DynamicResource EntryLabelVerticalAlignment}" />
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="{DynamicResource EntryValueHorizontalAlignment}" VerticalAlignment="{DynamicResource EntryValueVerticalAlignment}" DataContext="{Binding NombreInitialAssures}">
                            <Theme:NumericEditor 
                            Style="{StaticResource PercentageEditor}" 
                            Value="{Binding UniqueValue, UpdateSourceTrigger=LostFocus}"
                            IsEnabled="{Binding NotUsedSegmentation}"
                            Format="0 assurés"/>
                            <RadioButton DataContext="{Binding}" Margin="5" VerticalAlignment="Center" Checked="RadioButton_Checked" GroupName="Segments" IsThreeState="False" IsChecked="{Binding Selected, Mode=TwoWay}"/>

                            <ToggleButton  Style="{StaticResource SegmentEditorToggleButtonStyle}" IsChecked="{Binding NotUsedSegmentation}" Unchecked="ToggleButton_Unchecked" />
                        </StackPanel>
                    </UniformGrid>
                    <UniformGrid Margin="{DynamicResource EntryGridMargin}" Columns="2" IsEnabled="False" Visibility="Hidden" >
                        <TextBlock Text="Capital initial moyen" Margin="{DynamicResource EntryLabelMargin}" HorizontalAlignment="{DynamicResource EntryLabelHorizontalAlignment}" VerticalAlignment="{DynamicResource EntryLabelVerticalAlignment}" />
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="{DynamicResource EntryValueHorizontalAlignment}" VerticalAlignment="{DynamicResource EntryValueVerticalAlignment}" DataContext="{Binding CapitalInitialMoyen}">
                            <Theme:NumericEditor 
                            Style="{StaticResource PercentageEditor}" 
                            Value="{Binding UniqueValue, UpdateSourceTrigger=LostFocus}"
                            IsEnabled="{Binding NotUsedSegmentation}"
                            Format="0 euros"/>
                            <RadioButton DataContext="{Binding}" Margin="5" VerticalAlignment="Center" Checked="RadioButton_Checked" GroupName="Segments" IsThreeState="False" IsChecked="{Binding Selected, Mode=TwoWay}"/>


                            <ToggleButton  Style="{StaticResource SegmentEditorToggleButtonStyle}" IsChecked="{Binding NotUsedSegmentation}" Unchecked="ToggleButton_Unchecked"  />
                        </StackPanel>
                    </UniformGrid>
                </StackPanel>
            </GroupBox>
            <GroupBox Grid.Row="1" 
                      Style="{DynamicResource OMANE_GpeBox}" 
                      Header="Taux de Commission" 
                      Margin="10" 
                      Focusable="True" 
                      IsTabStop="True" 
                      TabIndex="0" 
                      Height="Auto">
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                    <UniformGrid Margin="{DynamicResource EntryGridMargin}" Columns="2"  >
                        <TextBlock Text="Taux de commission" Margin="{DynamicResource EntryLabelMargin}" HorizontalAlignment="{DynamicResource EntryLabelHorizontalAlignment}" VerticalAlignment="{DynamicResource EntryLabelVerticalAlignment}" />
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="{DynamicResource EntryValueHorizontalAlignment}" VerticalAlignment="{DynamicResource EntryValueVerticalAlignment}" DataContext="{Binding TauxCommissionSegmente}">
                            <Theme:NumericEditor Style="{StaticResource PercentageEditor}" Value="{Binding UniqueValue, UpdateSourceTrigger=LostFocus}" IsEnabled="{Binding NotUsedSegmentation}"/>
                            <RadioButton DataContext="{Binding}" Margin="5" VerticalAlignment="Center" Checked="RadioButton_Checked" GroupName="Segments" IsThreeState="False" IsChecked="{Binding Selected, Mode=TwoWay}" Focusable="False" IsTabStop="False"/>
                            <ToggleButton  Style="{StaticResource SegmentEditorToggleButtonStyle}" IsChecked="{Binding NotUsedSegmentation}" Unchecked="ToggleButton_Unchecked" Focusable="False" IsTabStop="False" />
                        </StackPanel>
                    </UniformGrid>
                </StackPanel>
            </GroupBox>
        </Grid>
        <GridSplitter HorizontalAlignment="Stretch" Grid.Row="1" Grid.ColumnSpan="3" ResizeDirection="Rows" IsTabStop="False" Focusable="False" />
        <GridSplitter ResizeDirection="Columns" HorizontalAlignment="Stretch" Grid.Row="2" Grid.Column="1" IsTabStop="False" Focusable="False" />

        <DockPanel Grid.Row="2" 
                    Visibility="Visible" 
                    DataContext="{Binding CurrentSegmentableValue}"
                    >
            <DockPanel.Resources>
                <CollectionViewSource x:Key="VariablesView"  Source="{Binding AvailableVariables}">
                    <CollectionViewSource.GroupDescriptions>
                        <PropertyGroupDescription PropertyName="Type"></PropertyGroupDescription>
                    </CollectionViewSource.GroupDescriptions>
                </CollectionViewSource>
            </DockPanel.Resources>
            <TextBlock TextWrapping="Wrap" Text="Variables de  Segmentation" Margin="5,5,0,5" DockPanel.Dock="Top"/>
            <ListView x:Name="variablesLst" 
                      BorderBrush="{x:Null}"       
                      ItemsSource="{Binding Source={StaticResource VariablesView}}" 
                      DockPanel.Dock="Bottom"
                      HorizontalContentAlignment="Stretch" SelectionMode="Multiple">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Description}" />
                    </DataTemplate>
                </ListView.ItemTemplate>
                <ListView.ItemContainerStyle>
                    <Style TargetType="{x:Type ListViewItem}">
                        <EventSetter Event="UIElement.PreviewMouseLeftButtonDown" Handler="VariableStyle_PreviewMouseLeftButtonDown" />
                        <Setter Property="HorizontalAlignment" Value="Stretch" />
                    </Style>
                </ListView.ItemContainerStyle>

                <ListView.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.ContainerStyle>
                            <Style TargetType="{x:Type GroupItem}">
                                <Setter Property="Margin" Value="0,0,0,5"></Setter>
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type GroupItem}">
                                            <Expander IsExpanded="True" BorderBrush="#FFA4B97F" BorderThickness="0,0,0,1">
                                                <Expander.Header>
                                                    <DockPanel>
                                                        <TextBlock FontWeight="Bold" Text="{Binding Path=Name, Converter={StaticResource TypeNameConverter}}"
                                                                   Margin="5,0,0,0" Width="100" />
                                                        <TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}" />
                                                    </DockPanel>
                                                </Expander.Header>
                                                <Expander.Content>
                                                    <ItemsPresenter />
                                                </Expander.Content>
                                            </Expander>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>

                        </GroupStyle.ContainerStyle>
                    </GroupStyle>
                </ListView.GroupStyle>
                <ListView.View>
                    <GridView>
                        <GridView.Columns>
                            <GridViewColumn Header="                       ">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <Image Margin="0,0,5,0" Source="{Binding Type, Converter={StaticResource TypeNameToIconConverter}}"></Image>
                                            <TextBlock FontWeight="Regular" Text="{Binding Description}"></TextBlock>
                                        </StackPanel>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                        </GridView.Columns>
                    </GridView>
                </ListView.View>
            </ListView>
        </DockPanel>
        <DockPanel 
            Grid.Row="2" 
            Grid.Column="2" 
            DataContext="{Binding CurrentSegmentableValue}" 
            Name="segmentsDockPanel">
            <TextBlock TextWrapping="Wrap"  Margin="5" DockPanel.Dock="Top">
                <TextBlock.Inlines>
                    <Run Text="Paramétrage : " />
                    <Run Text="{Binding Name, FallbackValue=Defaut}" />
                </TextBlock.Inlines>
            </TextBlock>
            <DataGrid                
                DockPanel.Dock="Bottom" 
                AllowDrop="True"  
                ItemsSource="{Binding Table}"
                ColumnWidth="*"
                Drop="DataGrd_Drop"               
                AutoGenerateColumns="True" 
                CanUserAddRows="False" 
                CanUserDeleteRows="False" 
                Name="segmentsGrd"
                >
                <DataGrid.ColumnHeaderStyle>
                    <Style TargetType="{x:Type DataGridColumnHeader}">
                        <Setter Property="VerticalContentAlignment" Value="Center"/>
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="Auto" />
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Grid.Column="0" Text="{Binding}"  HorizontalAlignment="Center" VerticalAlignment="Center" />
                                        <Button Grid.Column="1" 
                                                Content="X" 
                                                Padding="2" 
                                                DataContext="{Binding}" 
                                                BorderThickness="0"
                                                Click="DeleteButton_Click"
                                               >
                                            <Button.Visibility>
                                                <Binding RelativeSource="{RelativeSource AncestorType=DataGridColumnHeader}" Path="Column.IsReadOnly" >
                                                    <Binding.Converter>
                                                        <BooleanToVisibilityConverter />
                                                    </Binding.Converter>
                                                </Binding>
                                            </Button.Visibility>
                                            <Button.Style>
                                                <Style TargetType="Button">
                                                    <Setter Property="Template">
                                                        <Setter.Value>
                                                            <ControlTemplate TargetType="Button">
                                                                <Border  Width="{TemplateBinding ActualHeight}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" BorderBrush="Black" CornerRadius="4">
                                                                    <ContentPresenter HorizontalAlignment="Center"  />
                                                                </Border>
                                                            </ControlTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                    <Style.Triggers>
                                                        <Trigger Property="IsMouseOver" Value="True">
                                                            <Setter Property="BorderThickness" Value="2" />
                                                        </Trigger>
                                                    </Style.Triggers>
                                                </Style>
                                            </Button.Style>
                                        </Button>
                                    </Grid>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </DataGrid.ColumnHeaderStyle>

            </DataGrid>
        </DockPanel>
    </Grid>
</UserControl>
